package cn.hx.controller.system;

import cn.hx.entity.Dept;
import cn.hx.entity.Item;
import cn.hx.entity.ItemGroup;
import cn.hx.service.DeptService;
import cn.hx.service.ItemGroupService;
import cn.hx.service.ItemService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.Arrays;

@Controller
public class ItemContrller {
    @Resource
    private ItemService itemService;
    @Resource
    private DeptService deptService;
    @Resource
    private ItemGroupService groupService;


    @GetMapping("system/item/item")
    private String groupList(Item item,
                             ModelMap modelMap,
                             @RequestParam(defaultValue = "1") int page,
                             @RequestParam(defaultValue = "10") int size){
        modelMap.addAttribute("item",itemService.page(item,page, size));
        return "system/item/item";
    }
    @GetMapping("system/item/item_list/insert")
    public ModelAndView insert(Dept dept,ItemGroup itemGroup){
        ModelAndView modelAndView = new ModelAndView("system/item/item_save");
        modelAndView.addObject("depts",deptService.queryAllByLimit(dept,0,200));
        modelAndView.addObject("groups",groupService.queryAllByLimit(itemGroup,0,200));
        return modelAndView;
    }
    @GetMapping("system/item/item_change/{id}")
    public ModelAndView itemChange(@PathVariable Integer id){
        ModelAndView modelAndView = new ModelAndView("system/item/item_update");
        modelAndView.addObject("item",itemService.queryById(id));

        System.out.println(deptService.queryAllByLimit(new Dept(), 0, 200));
        modelAndView.addObject("depts",deptService.queryAllByLimit(new Dept(),0,200));

        modelAndView.addObject("groups",groupService.queryAllByLimit(new ItemGroup(),0,200));
        return modelAndView;
    }
    @PostMapping ("system/item/item_change/update/{id}")
    public String update(@PathVariable Integer id,Item item){
        item.setId(id);
        itemService.update(item);
        return "redirect:/system/item/item";
    }

    @PostMapping("system/item/item_list/save")
    public String save(Item item){
        String s = randomWord();
        item.setCode(s);
        itemService.insert(item);
        return "redirect:/system/item/item";
    }
    @GetMapping("system/item/del/{id}")
    public String del(@PathVariable Integer id){
        Item item = new Item();
        item.setId(id);
        item.setStatus(0);
        itemService.update(item);
        return "redirect:/system/item/item";
    }
    /**
     * 随机生成事项编码
     * @return 事项编码
     */
    public String randomWord(){
        String str = "";
        Integer range = 16;//随机生成一个八位数的字符串
        char[] arr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

        for(int i=0; i<range; i++){
            int round = (int)Math.round(Math.random() * (arr.length - 1));
            if(i!=0&&i%4==0){
                str += "-";
            }
            str += arr[round];
        }
        return "["+str+"]";
    }
}
